/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxinline.manager.pesquisar;

import br.com.foxinline.servico.ServicoGenerico;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.primefaces.component.datatable.DataTable;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 *
 * @author laverson
 */
public abstract class ManagerPesquisarGenerico<T> implements Serializable {

    private volatile ServicoGenerico servico;
    protected T entidadePesquisar;
    protected T entidadeRemover;
    protected DataTable searchResults;
    protected LazyDataModel<T> dataModel;

    @PostConstruct
    public abstract void init();

    public ServicoGenerico getServico() {
        return servico;
    }

    public void setServico(ServicoGenerico servico) {
        this.servico = servico;
    }

    public T getEntidadePesquisar() {
        return entidadePesquisar;
    }

    public void setEntidadePesquisar(T entidadePesquisar) {
        this.entidadePesquisar = entidadePesquisar;
    }

    public T getEntidadeRemover() {
        return entidadeRemover;
    }

    public void setEntidadeRemover(T entidadeRemover) {
        this.entidadeRemover = entidadeRemover;
    }

    public LazyDataModel<T> getDataModel() {
        return dataModel;
    }

    public void setDataModel(LazyDataModel<T> dataModel) {
        this.dataModel = dataModel;
    }

    public DataTable getSearchResults() {
        return searchResults;
    }

    public void setSearchResults(DataTable searchResults) {
        this.searchResults = searchResults;
    }

    public void pesquisar() {
        getSearchResults().setFirst(0);
        onLoadModel();
    }

    public void onLoadModel() {
        setDataModel(new LazyDataModel<T>() {
            private static final long serialVersionUID = -4947818379131191002L;

            @Override
            public List<T> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {

                if (first != 0) {
//                    first = first + pageSize;
                    getSearchResults().setFirst(first);
                }

                List<T> results = servico.pesquisar(entidadePesquisar, first, pageSize);

                if (results != null && results.isEmpty()) {
                    this.setRowCount(0);
                } else {

                    int value = servico.quantidadeRegistros().intValue();

                    if (value < 5) {
                        this.setRowCount(value);
                    } else if (value % 5 == 0) {
                        if (value - 5 != 0) {
                            this.setRowCount(value - pageSize);
                        } else {
                            this.setRowCount(value);
                        }
                    } else {
                        if (value >= pageSize) {
//                            this.setRowCount(value - pageSize - 1);
                            this.setRowCount(value);
                        } else {
                            this.setRowCount(pageSize);
                        }
                    }
                }


                return results;
            }

            @Override
            public void setRowIndex(int rowIndex) {
                if (rowIndex == -1 || getPageSize() == 0) {
                    super.setRowIndex(-1);
                } else {
                    super.setRowIndex(rowIndex % getPageSize());
                }
            }
        });
    }

    public abstract void limpar();

    public abstract void remover();

    public boolean dataModelIsEmpty() {
        return dataModel == null || dataModel.getRowCount() == 0;
    }
}
